package com.jadesoft.stock;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
  
public class SendMail implements Runnable {  
    private final String smtp_host = "smtp.gmail.com";                        
    private final String from_userName = "yangzt23@gmail.com";          
    private final String from_passWord = "yangzhiting";                        
    private final String show_name = "杨之廷";  
      
    public String email_address;  
    public String email_subject;  
    public String email_content;      
      
    public SendMail(String email_address, String email_subject, String email_content) {  
        this.email_address = email_address;  
        this.email_subject = email_subject;  
        this.email_content = email_content;  
    }  
  
    public void run() {  
        executeMailSend(email_address, email_subject, email_content);  
    }  
      
    /** 
     *  
     * @param recipients: the mail send to 
     * @param sendSubject: the mail subject 
     * @param sendText: the mail content 
     */  
    public void executeMailSend(String recipients, String sendSubject, String sendText) {  
          
        try {             
            Properties props = System.getProperties();  
            props.put("mail.smtp.starttls.enable", "true");  
            props.put("mail.smtp.host", smtp_host);  
            props.put("mail.smtp.user", from_userName);  
            props.put("mail.smtp.password", from_passWord);  
            props.put("mail.smtp.port", "587");  //gmail smtp port 587  
            props.put("mail.smtp.auth", "true");  
              
  
              
            Session session = Session.getDefaultInstance(props, null);  
            session.setDebug(false);  
              
            MimeMessage message = new MimeMessage(session);  
            message.setFrom(new InternetAddress(from_userName));  
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipients));  
            message.setFrom(new InternetAddress(show_name + "<" + from_userName + ">"));    
            message.setSubject(sendSubject);  
            message.setContent(sendText, "text/html;charset=utf-8");              
              
            Transport transport = session.getTransport("smtp");  
            transport.connect(smtp_host, from_userName, from_passWord);  
            transport.sendMessage(message, message.getAllRecipients());  
            transport.close();  
              
            System.out.println("send an email to " + recipients + " success");  
        } catch (Exception e) {  
            e.printStackTrace();  
            System.out.println("failure! ");  
  
        }  
    }  
    public static void main (String args[]){  
    	long a = System.currentTimeMillis();
        new SendMail("chunfengxixi@gmail.com","test111","<a href='http://baidu.com/'>baidu</a>").run();  
        System.out.println(System.currentTimeMillis()-a);
    }  
}  